package com.soulkun.smarthome.ui.main.home.adapter

import android.content.Context
import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ViewDataBinding
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppRecycleItemMainHomeSpeechRecognitionCommandListBinding
import com.soulkun.smarthome.logic.vo.SpeechRecognitionCommandVO
import soulkun.library.mvvm.adapter.BaseDataBindingRecyclerViewAdapter

class AppMainHomeSpeechRecognitionCommandListAdapter(
    context: Context,
    dataList: ObservableArrayList<SpeechRecognitionCommandVO>,
    private val onPostCommandListener: (SpeechRecognitionCommandVO) -> Unit
) : BaseDataBindingRecyclerViewAdapter<SpeechRecognitionCommandVO>(context, dataList) {

    override fun getContentLayoutResId(): Int {
        return R.layout.app_recycle_item_main_home_speech_recognition_command_list
    }

    override fun setContentViewHolderCreateListener(binding: ViewDataBinding) {
        (binding as AppRecycleItemMainHomeSpeechRecognitionCommandListBinding).onClickListener = View.OnClickListener {
            dataList.find { it.isSelected.get() }?.isSelected?.set(false)
            binding.speechRecognitionCommandVO!!.isSelected.set(true)
            onPostCommandListener(binding.speechRecognitionCommandVO!!)
        }
    }

    override fun setContentViewHolderBindListener(binding: ViewDataBinding, dataPosition: Int) {
        (binding as AppRecycleItemMainHomeSpeechRecognitionCommandListBinding).speechRecognitionCommandVO = dataList[dataPosition]
        if (dataList.size == 1) {
            onPostCommandListener(binding.speechRecognitionCommandVO!!)
        }
        binding.clMainHomeSpeechRecognitionCommandListLayoutContainer.setBackgroundResource(
            when (dataPosition % 4) {
                1 -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_green_a9
                }
                2 -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_red_5c
                }
                3 -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_blue_c6
                }
                else -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_purple_ca
                }
            }
        )
    }
}